//
//  UIBarButtonItem+Extension.swift
//  新浪微博
//
//  Created by Mac on 17/1/3.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

extension UIBarButtonItem{

    // 自定义view 生成item
    convenience init(imageName:String, imageNameSel:String, title:String?, textColor: UIColor = UIColor.darkGray, target:Any?, action:Selector?) {
        
        // 创建按钮,设置图片
        let button = UIButton()
        
        // 设置图片
        button.setImage(UIImage(named:imageName), for: .normal)
        button.setImage(UIImage(named:imageNameSel), for: .highlighted)
        
        //设置文字信息
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.lightGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(textColor, for: .normal)
        
        // 设置按钮点击监听
        if action != nil {
            button.addTarget(target, action: action!, for: .touchUpInside)
        }
        
        button.sizeToFit()
        
        self.init()
        self.customView = button
    }
    
    convenience init(backImage: UIImage?, backImageHighLighted: UIImage?, backImageDisable: UIImage?, size:CGSize, title: String?, textColor:UIColor = UIColor.darkGray, fontSize: CGFloat,target:Any?, action:Selector?) {
        
        // 创建按钮,设置图片
        let button = UIButton()
        button.frame.size = size
        
        // 设置图片
        button.setBackgroundImage(backImage, for: .normal)
        button.setBackgroundImage(backImageHighLighted, for: .highlighted)
        button.setBackgroundImage(backImageDisable, for: .disabled)
        
        //设置文字信息
        button.setTitle(title, for: .normal)
        button.setTitleColor(UIColor.lightGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        button.setTitleColor(textColor, for: .normal)
        
        // 设置按钮点击监听
        if action != nil {
            button.addTarget(target, action: action!, for: .touchUpInside)
        }
        
//        button.sizeToFit()
        
        self.init()
        self.customView = button
    }
    
}
